package com.jiankangjin.transsystem.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiankangjin.trading.dao.IChannelDao;
import com.jiankangjin.trading.dao.IOrderAffiliatedDao;
import com.jiankangjin.trading.dao.IOrderDataDao;
import com.jiankangjin.trading.dao.IOrderDetailDataDao;
import com.jiankangjin.trading.dao.IPaymentDataDao;
import com.jiankangjin.trading.entity.ChannelEntity;
import com.jiankangjin.trading.entity.OrderAffiliatedEntity;
import com.jiankangjin.trading.entity.OrderDataEntity;
import com.jiankangjin.trading.entity.OrderDetailDataEntity;
import com.jiankangjin.trading.entity.PaymentDataEntity;
import com.jiankangjin.transsystem.common.Constant;
import com.jiankangjin.transsystem.dao.ISearchOrderIdsDao;
import com.jiankangjin.transsystem.search.DataBaseMap;
import com.jiankangjin.transsystem.search.GetOrderRefundResult;
import com.jiankangjin.transsystem.search.GetOrderResult;
import com.jiankangjin.transsystem.services.SearchOrderServices;
import com.jiankangjin.transsystem.util.StringUtil;

/**  
 * 搜索出符合条件的所有订单ID
 */
@Service
public class SearchOrderServicesImpl implements SearchOrderServices {
	
	@Autowired
	private ISearchOrderIdsDao searchOrderIdsDao;
	
	@Autowired
	private IOrderDataDao orderDataDao;
	
	@Autowired
	private IPaymentDataDao paymentDataDao;
	
	@Autowired
	private IOrderAffiliatedDao orderAffiliatedDao;
	
	@Autowired
	private IOrderDetailDataDao orderDetailDataDao;
	
	@Autowired
	private IChannelDao channelDao;
	
	@Override
	public List<Map<String, Object>> searchOrderIds(
			String order_id,
			String order_date_begin,
			String order_date_end,
			String refund_date_begin,
			String refund_date_end,
			String search_word,
			String order_stat,
			String isTip) {
		HashMap<String, String> params=new HashMap<String, String>();
		params.put("order_id", order_id);
		params.put("order_date_begin", order_date_begin);
		params.put("order_date_end", order_date_end);
		params.put("refund_date_begin", refund_date_begin);
		params.put("refund_date_end", refund_date_end);
		params.put("search_word", "%" + search_word + "%");
		params.put("order_stat", order_stat);
		params.put("isTip", isTip);
		return searchOrderIdsDao.searchOrderIds(params);
	}

	/**
	 * 根据符合条件的ID，查出支付成功订单的信息。
	 */
	@Override
	public List<GetOrderResult> getOrderResult(
			String result_id,
			Integer page,
			Integer quantity) {
		List<String> IDs = DataBaseMap.get(result_id, page, quantity);
		List<GetOrderResult> orderResultList = new ArrayList<GetOrderResult>(IDs.size());
		//取出每个订单ID对应的GetOrderResult数据
		GetOrderResult or = null;
		for (int i = 0; i < IDs.size(); i++) {
			//--基本信息
			String order_id = IDs.get(i);
			or = new GetOrderResult();
			or.setOrder_id(order_id);
			OrderDataEntity oEntity = orderDataDao.selectByPrimaryKey(order_id);
			if (oEntity != null) {
				or.setOrder_status(oEntity.getOrdStat());
				or.setOrder_fee(oEntity.getOrdAmt());
				or.setOrder_pay_status(oEntity.getPayStat());
				or.setOrder_currency(oEntity.getOrdCry());
				or.setOrder_time(oEntity.getOrdRegTime());
				or.setOrd_fin_time(oEntity.getOrdFinTime());
				or.setPay_amt(oEntity.getPayAmt());
				or.setRefund_amt(oEntity.getRefundAmt());
				or.setOperator_id(oEntity.getOperatorId());
				or.setOperator_name(oEntity.getOperatorName());
				or.setOperator_id(oEntity.getOperatorId());
				or.setOperator_name(oEntity.getOperatorName());
			}
			//---交易信息
			PaymentDataEntity payE = new PaymentDataEntity();
			payE.setOrdSysId(order_id);
			payE.setPayMthd(Constant.PAY_METHOD_0001);
			List<PaymentDataEntity> payList = paymentDataDao.listByEntity(payE);
			List<GetOrderResult.Channel> channelList = new ArrayList<GetOrderResult.Channel>();
			GetOrderResult.Channel ch = null;
			if (payList != null) {
				for (PaymentDataEntity pay : payList) {
					ch = or.new Channel();
					ch.setSub_deal_ssn(pay.getId());
					ch.setSub_deal_stauts(pay.getPayStat());
					ch.setChannel_id(pay.getPayChnl());
					ChannelEntity chEntity = null;
					chEntity = channelDao.selectByPrimaryKey(pay.getPayChnl());
					if (chEntity != null) {
						ch.setChannel_name(chEntity.getChannelName());
						ch.setChannel_fee_type(chEntity.getChannelFeeType());
					}
					ch.setChannel_type(pay.getPayMthd());
					ch.setChannel_fee(pay.getPayAmt());
					ch.setChannel_currency(pay.getOrdCry());
					ch.setPay_user(pay.getPayUser());
					ch.setRefund_status(pay.getRefundStat());
					ch.setRefund_amt(pay.getRefundAmt());
					channelList.add(ch);
				}
				or.setChannel(channelList);
			}
			//--附属表中取出is_tip
			OrderAffiliatedEntity oa = new OrderAffiliatedEntity();
			oa.setOrderId(order_id);
			OrderAffiliatedEntity oaEntity = orderAffiliatedDao.selectByEntity(oa);
			if (oaEntity != null) {
				if (StringUtil.isNotEmpty(oaEntity.getIsTip())) {
					or.setIs_tip(oaEntity.getIsTip().equals("0") ? false : true);
				}
				or.setBuyer_name(oaEntity.getBuyerName());
				or.setBuyer_cellphone(oaEntity.getBuyerCellphone());
				or.setOrder_total_fee(oaEntity.getOrderTotalFee());
				or.setDiscount_fee(oaEntity.getDiscountFee());
				or.setDiscount_comment(oaEntity.getDiscountComment());
				or.setComment(oaEntity.getOrderComment());
			}
			//--商品详情信息
			OrderDetailDataEntity deEntity = new OrderDetailDataEntity();
			deEntity.setOrdId(order_id);
			List<OrderDetailDataEntity> detailList = orderDetailDataDao.listByEntity(deEntity);
			List<GetOrderResult.Goods> detail = new ArrayList<GetOrderResult.Goods>();
			GetOrderResult.Goods good = null;
			if (detailList != null) {
				for (OrderDetailDataEntity deta : detailList) {
					good = or.new Goods();
					good.setType(deta.getWaresType());
					good.setId(deta.getWaresId());
					good.setGoods_name(deta.getWaresName());
					good.setGoods_quantity(""+deta.getQuantity());
					good.setGoods_price(""+deta.getPrice());
					good.setStd(deta.getStd());
					good.setGoods_url(deta.getShowUrl());
					detail.add(good);
				}
				or.setDetail(detail);
			}
			
			//or.setComment("退款的原因，在申请退款时没有入库");
			orderResultList.add(or);
		}
		return orderResultList;
	}

	/**
	 * 根据符合条件的ID，查出有退款状态的订单信息
	 */
	@Override
	public List<GetOrderRefundResult> getOrderRefundResult(
			String result_id,
			Integer page,
			Integer quantity) {
		List<String> IDs = DataBaseMap.get(result_id, page, quantity);
		List<GetOrderRefundResult> orderRefundList = new ArrayList<GetOrderRefundResult>(IDs.size());
		//取出每个订单ID对应的GetOrderResult数据
		GetOrderRefundResult or = null;
		for (int i = 0; i < IDs.size(); i++) {
			//--基本信息
			String order_id = IDs.get(i);
			or = new GetOrderRefundResult();
			or.setOrder_id(order_id);
			OrderDataEntity oEntity = orderDataDao.selectByPrimaryKey(order_id);
			if (oEntity != null) {
				or.setOrder_status(oEntity.getOrdStat());
				or.setOrder_fee(oEntity.getOrdAmt());
				or.setOrder_pay_status(oEntity.getPayStat());
				or.setOrder_currency(oEntity.getOrdCry());
				or.setOrder_time(oEntity.getOrdRegTime());
				or.setOrd_fin_time(oEntity.getOrdFinTime());
				or.setPay_amt(oEntity.getPayAmt());
				or.setRefund_amt(oEntity.getRefundAmt());
				or.setRefund_start_time(oEntity.getRefundStartTime());
				or.setRefund_end_time(oEntity.getRefundEndTime());
				or.setOperator_id(oEntity.getOperatorId());
				or.setOperator_name(oEntity.getOperatorName());
			}
			//---交易信息
			PaymentDataEntity payE = new PaymentDataEntity();
			payE.setOrdSysId(order_id);
			payE.setPayMthd(Constant.PAY_METHOD_0002);
			payE.setRefundStat(Constant.REFUND_STAT_CODE_APPLIED);
			List<PaymentDataEntity> payList = paymentDataDao.listByEntity(payE);//申请退款的交易
			payE.setRefundStat(Constant.REFUND_STAT_CODE_SUCCESS);
			List<PaymentDataEntity> payList2 = paymentDataDao.listByEntity(payE);//退款完成的交易
			payList.addAll(payList2);
			List<GetOrderRefundResult.Channel> channelList = new ArrayList<GetOrderRefundResult.Channel>();
			GetOrderRefundResult.Channel ch = null;
			if (payList != null) {
				for (PaymentDataEntity pay : payList) {
					ch = or.new Channel();
					ch.setSub_deal_ssn(pay.getId());
					ch.setSub_deal_stauts(pay.getPayStat());
					ch.setChannel_id(pay.getPayChnl());
					ChannelEntity chEntity = null;
					chEntity = channelDao.selectByPrimaryKey(pay.getPayChnl());
					if (chEntity != null) {
						ch.setChannel_name(chEntity.getChannelName());
						ch.setChannel_fee_type(chEntity.getChannelFeeType());
					}
					ch.setChannel_type(pay.getPayMthd());
					ch.setChannel_fee(pay.getPayAmt());
					ch.setChannel_currency(pay.getOrdCry());
					ch.setPay_user(pay.getPayUser());
					ch.setRefund_status(pay.getRefundStat());
					ch.setRefund_amt(pay.getRefundAmt());
					ch.setApply_operator_id(pay.getApplyOperatorId());
					ch.setApply_operator_name(pay.getApplyOperatorName());
					ch.setApply_time(pay.getApplyTime());
					ch.setApply_comment(pay.getApplyComment());
					ch.setRefund_operator_id(pay.getRefundOperatorId());
					ch.setRefund_operator_name(pay.getRefundOperatorName());
					ch.setRefund_time(pay.getRefundTime());
					ch.setRefund_comment(pay.getRefundComment());
					channelList.add(ch);
				}
				or.setChannel(channelList);
			}
			//--附属表中取出is_tip
			OrderAffiliatedEntity oa = new OrderAffiliatedEntity();
			oa.setOrderId(order_id);
			OrderAffiliatedEntity oaEntity = orderAffiliatedDao.selectByEntity(oa);
			if (oaEntity != null) {
				or.setIs_tip(oaEntity.getIsTip().equals("0") ? false : true);
				or.setBuyer_name(oaEntity.getBuyerName());
				or.setBuyer_cellphone(oaEntity.getBuyerCellphone());
				or.setOrder_total_fee(oaEntity.getOrderTotalFee());
				or.setDiscount_fee(oaEntity.getDiscountFee());
				or.setDiscount_comment(oaEntity.getDiscountComment());
				or.setComment(oaEntity.getOrderComment());
			}
			//--商品详情信息
			OrderDetailDataEntity deEntity = new OrderDetailDataEntity();
			deEntity.setOrdId(order_id);
			List<OrderDetailDataEntity> detailList = orderDetailDataDao.listByEntity(deEntity);
			List<GetOrderRefundResult.Goods> detail = new ArrayList<GetOrderRefundResult.Goods>();
			GetOrderRefundResult.Goods good = null;
			if (detailList != null) {
				for (OrderDetailDataEntity deta : detailList) {
					good = or.new Goods();
					good.setType(deta.getWaresType());
					good.setId(deta.getWaresId());
					good.setGoods_name(deta.getWaresName());
					good.setGoods_quantity(""+deta.getQuantity());
					good.setGoods_price(""+deta.getPrice());
					good.setStd(deta.getStd());
					good.setGoods_url(deta.getShowUrl());
					detail.add(good);
				}
				or.setDetail(detail);
			}
			orderRefundList.add(or);
		}
		return orderRefundList;
	}
	
}
